import { AUTH_TOKEN_NAME, PERMISSIONS_NAME } from '@/config';
import { LOGIN_PATH } from '@/model/path';
import router from '@/router';

const setAuthToken = (auth_token: string | unknown, name?: string) => {
  if (!auth_token) clearAuthToken();
  window.localStorage.setItem(AUTH_TOKEN_NAME, auth_token as string);
  window.localStorage.setItem('name', name || 'unknown');
};
const setPermissions = (permissions: string[]) => {
  if (!permissions.length) clearPermissions();
  window.localStorage.setItem(
    PERMISSIONS_NAME,
    JSON.stringify(permissions) as string
  );
};
const clearAuthToken = () => {
  window.localStorage.removeItem(AUTH_TOKEN_NAME);
  window.localStorage.removeItem('name');
  router.replace(LOGIN_PATH);
};
const clearPermissions = () => {
  window.localStorage.removeItem(PERMISSIONS_NAME);
};
export { setAuthToken, clearAuthToken, setPermissions, clearPermissions };
